<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <el-form inline ref="searchForm">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="searchForm.real_name"
              placeholder="请输入用户名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              v-model="searchForm.phone"
              placeholder="请输入手机号码"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="小区名称" prop="communityid">
            <el-select v-model="searchForm.subdistrict_id">
              <el-option label="全部" value=""/>
              <el-option
                v-for="(oVal, oIdx) of communityList"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="searchForm.status">
              <el-option label="全部" value=""/>
              <el-option
                v-for="(oVal, oIdx) of status"
                :key="oIdx"
                v-bind="oVal"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList">查询</el-button>
          </el-form-item>
        </el-form>
        <div class="table_tool">
          <el-button type="success" @click="exportExcel">导出报名报表</el-button>
        </div>
      </template>

      <el-table :data="tableData" border id="out-table" stripe>
        <el-table-column prop="real_name" label="用户姓名" />
        <el-table-column prop="phone" label="用户手机号" />
        <el-table-column prop="identity_num" label="用户身份证号" />
        <el-table-column prop="sex" label="性别">
          <template slot-scope="{ row }">
            {{ row.sex==1?'男':'女' }}
          </template>
        </el-table-column>

        <el-table-column prop="subdistrict_name" label="所属小区" />
        <el-table-column prop="building_no" label="幢" />
        <el-table-column prop="unit" label="单元" />
        <el-table-column prop="room_num" label="室" />
        <el-table-column prop="face_img" label="人脸照片" align="center">
          <template slot-scope="{ row }">
            <el-image v-if="row.face_img"
              style="width: 100px; height: 100px"
              :src="row.face_img"
              :preview-src-list="[row.face_img]"
              />
            <div v-else>暂未上传</div>
          </template>
        </el-table-column>

      </el-table>
    </el-card>
    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="searchForm.page"
        :page-sizes="[20, 30, 50, 100]"
        :page-size="searchForm.page_size"
        :total="tableTotal"
        @size-change="changeSize"
        @current-change="changeCurrent"
      />
    </footer>

  </div>
</template>

<script>
import API from '@/api';
import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { table, addOrEdit } from "@/utils/mixins";
import { createNamespacedHelpers } from 'vuex'
const { mapState, mapActions } = createNamespacedHelpers('asyncStatus')
export default {
  mixins: [table, addOrEdit],
  data: () => ({
    showAddOrEdit: false,
    isLoading: true,
    tableData:[],
    tableTotal: 0,
    isKey: false,
    searchForm:{
      status:2,
      real_name:"",
      phone:"",
      subdistrict_id:"",
      page:1,
      per_page:20
    },
    status: [
      {
        value: 1,
        label: "待审核",
      },
      {
        value: 2,
        label: "通过",
      },
      {
        value: 3,
        label: "未通过",
      },
    ],
    sex:[
      {
        value: 1,
        label: "男",
      },
       {
        value: 2,
        label: "女",
      },
    ],
    communityList: [],
    buildingForm:{
      $id:"",
      real_name:"",
      phone:"",
      identity_num:"",
      sex:"",
      subdistrict_id:"",
      building_no:"",
      unit:"",
      room_num:"",

    }
  }),

  created() {
    this.getList()
    API.getCommunityList().then(({data})=>{
      this.communityList=data.items.map(item=>{
        return {value:item.id,label:item.name}
      })
    })
  },

  methods: {
    //列表
    getList(){
      API.getHouseholdAuditList(this.searchForm)
        .then(({ data }) => {
          data.items.forEach(item=>{
            item.sex=parseInt(item.sex)
            item.status=parseInt(item.status)
            item.phone=item.phone.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2')
            item.identity_num=item.identity_num.replace(/^(.{6})(?:\d+).(.{4})$/,'$1****$2')
            item.subdistrict_name=this.communityList.filter(sitem=>{
              return sitem.value==item.subdistrict_id
            })[0].label
          })
          console.log(data.items)
          this.tableData = data.items
          this.tableTotal = data.total
        })
        .catch((err) => {
          this.tableData = []
          this.tableTotal=0
        });
    },

    // 分页
    changeSize(val){
      this.searchForm.per_page = val
      this.searchForm.page = 1
      this.getList()
    },

    // 分页
    changeCurrent(val){
      this.searchForm.page = val
      this.getList()
    },

    // 打开个人信息弹窗
    openEditRole(id){
      this.buildingForm.$id=id
      API.getHouseholdAuditDetail(id).then(({data})=>{
        this.buildingForm.real_name=data.real_name,
        this.buildingForm.phone=data.phone,
        this.buildingForm.identity_num=data.identity_num,
        this.buildingForm.sex=data.sex,
        this.buildingForm.subdistrict_id=data.subdistrict_id,
        this.buildingForm.building_no=data.building_no,
        this.buildingForm.unit=data.unit,
        this.buildingForm.room_num=data.room_num
      }).catch((err) => {
      }).finally(()=>{
        this.showAddOrEdit=true
      });
    },

    // 关闭弹窗
    closeDialog(){
      this.showAddOrEdit=false
      this.restForm()
    },

    // 重置表单
    restForm(){
      this.buildingForm.real_name="",
      this.buildingForm.phone="",
      this.buildingForm.identity_num="",
      this.buildingForm.sex="",
      this.buildingForm.subdistrict_id="",
      this.buildingForm.building_no="",
      this.buildingForm.unit="",
      this.buildingForm.room_num=""
    },

    // 删除
    delRowById(id){
      API.deleteRealname(id).then(()=>{
        this.$message.success("删除成功")
        this.getList()
      })
    },

    exportExcel() {
      var wb = XLSX.utils.table_to_book(document.querySelector("#out-table"));
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
            new Blob([wbout], { type: "application/octet-stream" }),
            `人员信息导出数据.xlsx`
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

  },
};
</script>

<style lang="scss" scoped></style>
